package com.cmict.data.system.controller.admin.encode;

import com.cmict.data.framework.common.pojo.CommonResult;
import com.cmict.data.framework.common.pojo.PageResult;
import com.cmict.data.system.controller.admin.encode.vo.EncodeCreateReqVO;
import com.cmict.data.system.controller.admin.encode.vo.EncodeDataPageReqVO;
import com.cmict.data.system.dal.dataobject.encode.EncodeDO;
import com.cmict.data.system.service.encode.EncodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static com.cmict.data.framework.common.pojo.CommonResult.success;

@Api(tags = "管理后台 - 自动编码")
@RestController
@RequestMapping("/system/encode")
@Validated
public class EncodeController {

    @Resource
    private EncodeService encodeService;

    @PostMapping("/create")
    @ApiOperation("创建自动编码")
    @PreAuthorize("@ss.hasPermission('system:encode:create')")
    public CommonResult<Long> createEncode(@Valid @RequestBody EncodeCreateReqVO reqVO) {
        Long encodeId = encodeService.createEncodeData(reqVO);
        return success(encodeId);
    }

    @PostMapping("/update")
    @ApiOperation("修改自动编码")
    @PreAuthorize("@ss.hasPermission('system:encode:update')")
    public CommonResult<Boolean> updateEncode(@Valid @RequestBody EncodeCreateReqVO reqVO) {
        encodeService.updateEncodeData(reqVO);
        return success(true);
    }

    @GetMapping("/delete")
    @ApiOperation("删除自动编码")
    @ApiImplicitParam(name = "id", value = "主键", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('system:encode:delete')")
    public CommonResult<Boolean> deleteEncode(@RequestParam("id") Long id) {
        encodeService.deleteEncodeData(id);
        return success(true);
    }

    @PostMapping("/batchdelete")
    @ApiOperation("批量删除自动编码")
    @PreAuthorize("@ss.hasPermission('system:encode:delete')")
    public CommonResult<Boolean> batchDeleteEncode(@Valid @RequestBody List<Long> ids) {
        encodeService.deleteEncodeDataBatch(ids);
        return success(true);
    }

    @ApiOperation("/获得自动编码的分页列表")
    @GetMapping("/page")
    public CommonResult<PageResult<EncodeDO>> pageEncodes(@Valid EncodeDataPageReqVO reqVO) {
        return success(encodeService.getDictEncodePage(reqVO));
    }

    @ApiOperation("/查询自动编码详细")
    @ApiImplicitParam(name = "id", value = "主键", required = true, example = "1024", dataTypeClass = Long.class)
    @GetMapping(value = "/get")
    @PreAuthorize("@ss.hasPermission('system:encode:query')")
    public CommonResult<EncodeDO> getEncode(@RequestParam("id") Long id) {
        return success(encodeService.getEncodeData(id));
    }
}
